// myPages/discover/discover.js
const util = require('../../utils/util.js');
const api = require('../../config/api.js');

const app = getApp()
Page({
  data: {
    picUrl:'https://knowif.oss-cn-beijing.aliyuncs.com/88369c6da005770b43bf4a83cda3810.png',
    recommendTeacher: [],
    banner: [],
    project: [],
    recommendTeacher: [],
    ad: false,
    current_city: '',
    imgalist: ['https://knowif.oss-cn-beijing.aliyuncs.com/tutorPicture/%E5%B0%8F%E7%A8%8B%E5%BA%8Fbanner/%E5%BE%AE%E4%BF%A1%E5%9B%BE%E7%89%87_20190629223810.png'],
  },

  getIndexData: function () {
    let that = this;
    util.request(api.IndexUrl).then(function (res) {
      if (res.code === 200) {
        //设置介绍文字
        if (res.result.tutorInfos) {
          res.result.tutorInfos.forEach((value, index, arr) => {
            let userEducationExperiencesTXT = "";
            for (let i in value.userEducationExperiences) {
              if (i == value.userEducationExperiences.length - 1) {
                userEducationExperiencesTXT += value.userEducationExperiences[i].college
              } else {
                userEducationExperiencesTXT += value.userEducationExperiences[i].college + " | "
              }
            }
            value.userEducationExperiencesTXT = userEducationExperiencesTXT.substring(0, userEducationExperiencesTXT.length)
            userEducationExperiencesTXT = "";
          })
        }
        that.setData({
          banner: res.result.banners,
          project: res.result.items,
          recommendTeacher: res.result.tutorInfos
        });
      }
    });
  },

  goToTeacher: function (e) {
    console.log(e.currentTarget.dataset.id);
    if (e.currentTarget.dataset.id != undefined) {
      wx.navigateTo({
        url: '../../myPages/teacher/teacher?teacherId=' + e.currentTarget.dataset.id
      })
    }
  },
  goToSearch: function () {
    wx.navigateTo({
      url: '../../myPages/search/search'
    })
  },
  goToCitySearch: function () {
    wx.navigateTo({
      url: '../../myPages/CitySearch/search'
    })
  },
  goToTeacherDetail: function (e) {
    wx.navigateTo({
      url: '../../myPages/teacherDetail/teacherDetail?teacherId=' + e.currentTarget.dataset.id
    })
  },
  goToProject: function (e) {
    if (e.currentTarget.dataset.name == '短租公寓') {
      wx.previewImage({
        current: this.data.imgalist, // 当前显示图片的http链接   
        urls: this.data.imgalist // 需要预览的图片http链接列表   
      })
      // wx.getImageInfo({// 获取图片信息
      //   src: 'https://knowif.oss-cn-beijing.aliyuncs.com/tutorPicture/%E5%B0%8F%E7%A8%8B%E5%BA%8Fbanner/%E5%BE%AE%E4%BF%A1%E5%9B%BE%E7%89%87_20190629210916.png',
      //   success: function (res) {
      //   }
      // })
    } else {
      wx.navigateTo({
        url: e.currentTarget.dataset.url
      })
    }
  },
  goToRegister: function (e) {
    let _userId = wx.getStorageSync('userInfo').id;
    if (_userId == undefined) {
      wx.showToast({
        title: '请先去我的页面登录',
        icon: 'none'
      });
      return
    }
    util.request(api.TutorInfoByUserId, {
      userId: _userId
    }, 'POST').then(function (res) {
      if (res.code !== 200) {
        wx.navigateTo({
          url: '../../myPages/register/register'
        })
      } else {
        let userType = res.result.tUser.userType;
        if (userType == 2) {
          wx.navigateTo({
            url: `../../myPages/teacherDetail/teacherDetail?from=index`
          })
        } else if (userType == 3 || userType == 5) {
          wx.showToast({
            title: '您已经是导师啦，不用再发布啦！',
            icon: 'none'
          });
        } else {
          wx.showToast({
            title: '用户类型错误',
            icon: 'none'
          });
        }
      }
    })
  },
  goToProcess: function (e) {
    wx.navigateTo({
      url: '../../myPages/process/process'
    })
  },
  //图片预览
  previewImage: function (e) {
    console.log(e.currentTarget.dataset.linkurl)
    if (e.currentTarget.dataset.linkurl != '') {
      var url = [e.currentTarget.dataset.linkurl];
      wx.previewImage({
        current: url, // 当前显示图片的http链接   
        urls: url // 需要预览的图片http链接列表   
      })
      wx.getImageInfo({ // 获取图片信息
        src: url[0],
        success: function (res) { }
      })
    }
  },
  close() {
    this.setData({
      ad: false
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.getIndexData();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // 页面显示  城市定位信息
    let currentCity = app.globalData.currentCity;
    console.log(currentCity);
    this.setData({
      current_city: currentCity
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let that = this;
    return {
      title: '知识鸭', // 转发后 所显示的title
      path: '/myPages/Qask/Qask', // 相对的路径
      imageUrl: "/images/common/share_app.png",
      success: (res) => { // 成功后要做的事情
        wx.getShareInfo({
          shareTicket: res.shareTickets[0],
          success: (res) => { },
          fail: function (res) {
            console.log(res)
          },
          complete: function (res) {
            console.log(res)
          }
        })
      },
      fail: function (res) {
        // 分享失败
        console.log(res)
      }
    }
  },
})